<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authenticate extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }
    public function index() {
        if($this->session->userdata('barangay_id') == '' || $this->session->userdata('barangay_id') == null) {
            //$this->load->view('index_main');
            $this->load->view('index_main1');
        } else {
            $this->load->view('index_brgy');
        }
    }
    public function login() {
        if($this->session->userdata('barangay_id') == '' || $this->session->userdata('barangay_id') == null) {
            if($this->input->post('login')) {
                $this->load->model('Auth_model');
                $result = $this->Auth_model->login();
                if($result == "ok") {
                    header('Location: ' . base_url());
                } else {
                    echo $result;
                }
            } else {
                $this->load->view('admin_login');
            }
        } else {
            header('Location: ' . base_url() . 'index.php/index');
        }
    }
    public function logout() {
        if($this->session->userdata('barangay_id') != '' || $this->session->userdata('barangay_id') != null) {
            $this->load->model('Auth_model');
            $this->Auth_model->logout();
        }
        header('Location: ' . base_url());
    }
    public function changepassword() {
        if($this->session->userdata('user_resp') == "admin") {
            $this->load->model('Auth_model');
            $this->Auth_model->changePassword();
        }
    }
    public function onexit() {
        if($this->session->userdata('barangay_id') != '' || $this->session->userdata('barangay_id') != null) {
            $this->load->model('Auth_model');
            $this->Auth_model->logout();
        }
        header('Location: ' . base_url());
    }
    public function viewsite() {
        if($this->input->post('showbrgy')) {
            $this->load->model('Auth_model');
            $result = $this->Auth_model->viewSite();
            //print $result;
            if($result == "ok") {
                header('Location: ' . base_url());
            } else {
                header('Location: ' . base_url());
            }
        } else {
            header('Location: ' . base_url());
        }
    }
}